<template>
  <el-dialog title="参与人数" v-model="visible" width="1100px" @close="visible = false">
    <div class="main">
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="用户" prop="nickName">
          <el-input
            style="width: 200px"
            maxlength="16"
            v-model.trim="queryParams.nickName"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户账号" prop="mobile">
          <el-input
            style="width: 200px"
            maxlength="30"
            v-model.trim="queryParams.mobile"
            placeholder="请输入"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="参与点位" prop="positionId">
          <el-cascader
            ref="cascaderRef"
            placeholder="下拉模糊搜索"
            :options="positionOptions"
            clearable
            filterable
            :props="{ value: 'id', label: 'name', children: 'children' }"
            v-model="queryParams.positionId"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" index height="500" size="default" :data="dataList">
        <el-table-column type="index" :index="indexAdd" label="编号" width="50" align="center" />
        <el-table-column label="用户" align="center" prop="nickName" show-overflow-tooltip />
        <el-table-column label="用户账号" align="center" prop="mobile" />
        <el-table-column label="抽奖次数" align="center" prop="lotteryCount" />
        <el-table-column label="中奖次数" align="center" prop="winningCount" />
        <el-table-column label="获得奖励" align="center">
          <template #default="{ row }">
            <el-link :underline="false" type="primary" @click="handleReward(row)">详情</el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="参与点位"
          align="center"
          prop="positionName"
          width="150"
          show-overflow-tooltip
        />
      </el-table>
      <div style="right: 10px; position: relative">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <RewardDialog ref="rewardRef" />
  </el-dialog>
</template>

<script setup>
import RewardDialog from './rewardDialog.vue'
import { getAllPositions } from '@/api/association'
import { getJoinListApi } from '@/api/marketing/raffle'

const dataList = ref([])
const visible = ref(false)
const loading = ref(true)
const total = ref(0)

const queryRef = ref(null)
const positionOptions = ref([])
const queryParams = reactive({
  lotteryId: '',
  nickName: '',
  mobile: '',
  pageNum: 1,
  pageSize: 10
})

function init(row, cityName) {
  visible.value = true
  queryParams.lotteryId = row.id
  queryParams.cityName = cityName
  handleQuery()
}

function getPositions() {
  getAllPositions({ cityName: queryParams.cityName }).then((res) => {
    const data = res?.data || []
    positionOptions.value = data.map((item) => {
      if (item.children && item.children.length > 0) {
        item.children = item.children.map((item1) => {
          if (item1.children && item1.children.length > 0) {
            item1.children = item1.children.map((item2) => {
              if (!item2.children || item2.children.length === 0) {
                item2.disabled = true
              }
              return item2
            })
          } else {
            item1.disabled = true
          }
          return item1
        })
      } else {
        item.disabled = true
      }
      return item
    })
  })
}

function getList() {
  loading.value = true
  const params = { ...queryParams }
  params.positionId = queryParams.positionId?.length ? queryParams.positionId[3] : ''
  getJoinListApi(params)
    .then((res) => {
      loading.value = false
      dataList.value = res?.data?.rows || []
      total.value = res?.data?.total || 0
    })
    .catch(() => {
      loading.value = false
      dataList.value = []
    })
}

// 表格编号递增
function indexAdd(index) {
  const { pageNum, pageSize } = queryParams
  return index + 1 + (pageNum - 1) * pageSize
}

// 奖励详情
const rewardRef = ref(null)
function handleReward(row) {
  rewardRef?.value.init(row, queryParams.cityName, queryParams.lotteryId)
}

// 搜索
function handleQuery() {
  queryParams.pageNum = 1
  getPositions()
  getList()
}

// 重置
function resetQuery() {
  queryRef.value.resetFields()
  handleQuery()
}

defineExpose({
  init
})
</script>

<style lang="scss" scoped>
.main {
  min-height: 300px;
}
</style>
